package com.opal.modules.costReportForm.controller;


import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.domain.ReturnMessageData;
import com.opal.model.DataGridData;
import com.opal.modules.costReportForm.annotation.IsInitialInfo;
import com.opal.modules.costReportForm.entity.*;
import com.opal.modules.costReportForm.entity.table.CostBh;
import com.opal.modules.costReportForm.entity.table.CostReportInitialInfo;
import com.opal.modules.costReportForm.service.CostReportFormService;
import com.opal.utils.ResultMsg;
import com.opal.vo.GrfTableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/costReportForm")
public class CostReportFormController {

    @Autowired
    private CostReportFormService costReportFormService;

    /**
     * A水料价新增 修改
     * @param costReportA
     * @param costReportId
     * @return
     */
    @RequestMapping("/costReportASave")
    @ResponseBody
    public ReturnMessageData costReportASave(@RequestBody List<CostReportA> costReportA, String costReportId){
        return costReportFormService.costReportUpdate(costReportA,costReportId);
    }

    /**
     * B人工新增 修改
     * @param costReportB
     * @param costReportId
     * @return
     */
    @RequestMapping("/costReportBSave")
    @ResponseBody
    public ReturnMessageData costReportBSave(@RequestBody List<CostReportB> costReportB,String costReportId){
        return costReportFormService.costReportBUpdate(costReportB,costReportId);
    }

    /**
     * C配件费新增 修改
     * @param costReportC
     * @param costReportId
     * @return
     */
    @RequestMapping("/costReportCSave")
    @ResponseBody
    public ReturnMessageData costReportCSave(@RequestBody List<CostReportC> costReportC,String costReportId){
        return costReportFormService.costReportCUpdate(costReportC,costReportId);
    }

    /**
     * 人工工时单价：查询
     * @param costBh
     * @return
     */
    @RequestMapping("/selectTimePrice")
    @ResponseBody
    public Object selectTimePrice(CostBh costBh){
        return costReportFormService.selectTimePriceService(costBh);
    }

    /**
     * 人工工时单价：增加
     * @param costBh
     * @return
     */
    @RequestMapping("/addTimePrice")
    @ResponseBody
    public Object addTimePrice(CostBh costBh){
        return costReportFormService.addTimePriceService(costBh);
    }

    /**
     * 人工工时单价：修改
     * @param costBh
     * @return
     */
    @RequestMapping("/modifyTimePrice")
    @ResponseBody
    public Object modifyTimePrice(CostBh costBh){
        return costReportFormService.modifyTimePriceService(costBh);
    }

    /**
     *
     * @param costReport
     * @return
     */
    @RequestMapping("/updataCostReport")
    @ResponseBody
    public ResultMsg updataCostReport(CostReport costReport){
        return costReportFormService.updataCostReport(costReport);
    }


    //水料标准工时(easyui)
    @RequestMapping("/costReportInitialInfo")
    @ResponseBody
    public List<CostReportInitialInfoEayui> selectAll(@RequestParam("type") int type, String typeName,String num,String pkOrg){
        return costReportFormService.selectAll(type,typeName,num,pkOrg);
    }

    //水料标准工时(layui)
    @RequestMapping("/costReportInitialInfoLayui")
    @ResponseBody
    public List<CostReportInitialInfoLayui> selectAll2(@RequestParam("type") int type){
        return costReportFormService.selectAll2(type);
    }

    /**
     * 水料标准工时添加
     * @param costReportInitialInfo
     * @return
     */
    @RequestMapping("/costPswhAdd")
    @ResponseBody
    public Object costPswhAdd(CostReportInitialInfo costReportInitialInfo){
        return costReportFormService.costPswhAddService(costReportInitialInfo);
    }

    /**
     * 人工工时单价：获取详情
     * @return
     */
    @RequestMapping("/getPriceHours")
    @ResponseBody
    public Object getPriceHours(){
        return costReportFormService.getPriceHoursService();
    }

    /**
     * 产品标准工时：修改
     * @param costReportInitialInfo
     * @return
     */
    @IsInitialInfo   //自定义注解  判断是否能修改或删除
    @RequestMapping("/costPswhUpdate")
    @ResponseBody
    public Object costPswhUpdate(CostReportInitialInfo costReportInitialInfo){
        return costReportFormService.costPswhUpdateService(costReportInitialInfo);
    }

    /**
     * 产品标准工时：删除根目录
     * @param id
     * @return
     */
    @IsInitialInfo
    @RequestMapping("/costPswhDel")
    @ResponseBody
    public Object costPswhDel(Integer id){
        return costReportFormService.costPswhDelService(id);
    }

    @RequestMapping("/costReportASelect")
    @ResponseBody
    public ReturnMessageData costReportASelect(String costReportId){
        return costReportFormService.costReportASelect(costReportId);
    }

    //A水料价删除
    @RequestMapping("/costReportADetele")
    @ResponseBody
    public Map costReportADetele(Integer id){
        return costReportFormService.costReportADetele(id);
    }

    //B人工删除
    @RequestMapping("/costReportBDetele")
    @ResponseBody
    public Map costReportBDetele(Integer id){
        return costReportFormService.costReportBDetele(id);
    }


    //C配件费删除
    @RequestMapping("/costReportCDetele")
    @ResponseBody
    public Map costReportCDetele(Integer id){
        return costReportFormService.costReportCDetele(id);
    }

    //grf-查询返回数据
    @RequestMapping("/getCostReportGrf")
    @ResponseBody
    public GrfTableVO<Map> getCostReportGrf(String banben) throws IllegalAccessException {
        return costReportFormService.getCostReportGrf(banben);
    }

    //获取ABC信息
    @RequestMapping("/getCostReportABC")
    @ResponseBody       //2020年3月26日为了拷贝成本报告功能添加标识符copy
    public GrfTableVO<Map> getCostReportABC(String banben,String copy){
        return costReportFormService.getCostReportABC(banben,copy);
    }


    /**
     * 成本报告导出至excel表中
     * @param request
     * @param response
     */
    @RequestMapping("exportCostReportToExcel")
    public void exportCostReportToExcel(String costReportId, HttpServletRequest request, HttpServletResponse response){
        costReportFormService.exportCostReportToExcelService(costReportId,request,response);
    }

    //获取客户名称
    @RequestMapping("/getbdCustomerName")
    @ResponseBody
    public String getbdCustomerName(String code){
        return costReportFormService.selectName(code);
    }

    /**
     * 生成版本号（数字部分）
     * @return
     */
    @RequestMapping("/createGetcostReportId")
    @ResponseBody
    public String createGetcostReportId(){
        return costReportFormService.createGetcostReportId();
    }

    /**
     * 生成版本号（部门简写）
     * @return
     */
    @RequestMapping("/getDeptNano")
    @ResponseBody
    public Object getDeptNano(){
        return costReportFormService.getDeptNano();
    }

    /**
     * c 其它配件 模糊查询
     * @return
     */
    @RequestMapping("/selectSystemMountings")
    @ResponseBody
    public Object selectSystemMountings(String name, DataGridData<String> rows){
        return costReportFormService.selectSystemMountingsService(name,rows);
    }

    /**
     * 获取币种
     * @return
     */
    @RequestMapping("/getCurrency")
    @ResponseBody
    public Object getCurrency(){
        R r = (R) costReportFormService.getCurrencyService(null);
        List<Map<String,String>> list= (List<Map<String, String>>) r.getObj();
        if (list!=null && list.size()>0){
            Map<String, String> map=  new HashMap<>();
            map.put("CODE","RMB");
            map.put("RATE","1");
            map.put("NAME","人名币");
            list.add(0,map);
        }
        return r;
    }

    /**
     * 获取港币率
     * @return
     */
    @RequestMapping("/getHKDRate")
    @ResponseBody
    public Object getHKDRate(String code){
        R hkd = (R) costReportFormService.getCurrencyService(code);
        return hkd.getObj();
    }

    /**
     * A 水料价 根据输入配方号获取相关的价格
     * @return
     */
    @RequestMapping("/selectArticlePrice")
    @ResponseBody
    public Object selectArticlePrice(String peiFangHao){
        return costReportFormService.selectArticlePrice(peiFangHao);
    }

    /**
     * 根据 成本编码获取相关的规格、单位、C配件
     * @return
     */
    @RequestMapping("/selectCostCodingItem")
    @ResponseBody
    public Object selectCostCodingItem(String costCoding,String pkOrg){
        return costReportFormService.selectCostCodingItemService(costCoding,pkOrg);
    }

    /**
     *
     * 根据组织查询查询部门
     * @param pkHrorg
     * @return
     */
    @RequestMapping("/getCostPkOrg")
    @ResponseBody
    public Object getCostPkOrg(String pkHrorg){
        return costReportFormService.getCostPkOrgService(pkHrorg);
    }

    /**
     *
     * 获取计算【授权费\顾问\其他】计算详情
     * @param costReportId
     * @return
     */
    @RequestMapping("/getCalculateOtherChargesZtTableData")
    @ResponseBody
    public Object getCalculateOtherChargesZtTableData(String costReportId){
     return costReportFormService.getCalculateOtherChargesZtTableDataService(costReportId);
    }



    @RequestMapping("{page}")
    public String skipPage(@PathVariable("page") String page) {
        return "costReportForm/" + page;
    }
    @RequestMapping("otherCost/{page}")
    public String skipPage2(@PathVariable("page") String page) {
        return "costReportForm/otherCost/" + page;
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R costReportExcetion(Exception e){
        return  new R(WSC.REQUEST_ERROR,"系统维护中，请联系管理员！");
    }

}
